<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\App;
use think\facade\Db;
use util\Excel;

/**
 * Test控制器
 */
class Test extends AdminBase
{

    protected $model = null;

    protected $validate = null;

    protected $relationSearch = false;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Test();

        $this->validate = \app\admin\validate\Test::class;
    }


    /**
     * SKU排行
     * @param array params
     */
    public function sku_ranking()
    {
        //默认bra01 is_output 默认不导出
        $post = $this->request->post();
        $brand_name = !empty($post['brand_name']) ? $post['brand_name'] : 'bra01';
        $start_time = !empty($post['start_time']) ? strtotime($post['start_time']) : mktime(0, 0, 0, date('m'), 1, date('Y'));
        $end_time =  !empty($post['end_time']) ? strtotime($post['end_time']) : time();
        $is_output =  !empty($post['is_output']) ? strtotime($post['is_output']) : 0;
        $sql = "SELECT 
        c.sku,
        sum(b.quantity) as count,
        b.title,
        e.handle_url,
        b.product_id,
        c.variants_id as SKUid
        FROM qu_order as a
        LEFT JOIN qu_order_quantity as b ON a.id=b.order_id
        LEFT JOIN qu_order_sku as c ON b.variant_id=c.variants_id
        LEFT JOIN qu_order_product as e ON b.product_id=e.product_id
        where a.financial_status = 'paid'
        AND a.created_at >= " . $start_time . "
        AND a.created_at <= " . $end_time . "
        AND c.id > 1
        AND b.sku like '%{$brand_name}%'
        GROUP BY SKUid
        ORDER BY count DESC
    ";
        $join_datas = Db::query($sql);
        if (!$join_datas) {
            return [];
        }
        foreach ($join_datas as $k => $v) {
            $explode_arr = explode('-', strtolower($v['sku']));
            if (isset($explode_arr[1])) {
                $color[$k] = explode('+', $explode_arr[1]);
            }
        }
        foreach ($join_datas as $k => $v) {
            $explode_arr = explode('-', strtolower($v['sku']));
            if (isset($explode_arr[2])) {
                $size[$k] = $explode_arr[2];
            }
        }
        $color = $this->array_unique_fb($color);
        $size = array_unique($size);
        sort($size);
        asort($color);
        $merge_sku[$brand_name] = 0;
        foreach ($color as   $v) {
            $merge_sku[$brand_name . '_' . $v] = 0;
            foreach ($size as $vl) {
                $merge_sku[$brand_name . '_' . $v . '_' . $vl] = 0;
            }
        }
        // halt($color, $size, $merge_sku);

        //bra01-black+blue-xl-2
        foreach ($join_datas as $join_data) {
            //数量
            $multiple = 1;
            $sku_title_arr =  explode('-', strtolower($join_data['sku']));
            file_put_contents('testsku.txt', $join_data['sku']);
            if (isset($sku_title_arr[0])) {
                if (isset($sku_title_arr[1])) {
                    if (isset($sku_title_arr[3])) {
                        $multiple = $sku_title_arr[3];
                    }
                    $sku_title_arr1 =  explode('+', $sku_title_arr[1]);
                    foreach ($sku_title_arr1 as $vl) {
                        $merge_sku[$sku_title_arr[0]] = $merge_sku[$sku_title_arr[0]] + ((int)$join_data['count']) * $multiple;
                        $merge_sku[$sku_title_arr[0] . '_' . $vl] = $merge_sku[$sku_title_arr[0] . '_' . $vl] + ((int)$join_data['count']) * $multiple;
                        if (isset($sku_title_arr[2])) {
                            $merge_sku[$sku_title_arr[0] . '_' . $vl . '_' . $sku_title_arr[2]] = $merge_sku[$sku_title_arr[0] . '_' . $vl . '_' . $sku_title_arr[2]]  + ((int)$join_data['count']) * $multiple;
                        }
                    }
                }
            }
        }
        $merge_sku_new = array();
        foreach ($merge_sku as $k => $v) {
            $merge_sku_new[] =
                ['sku' => $k, 'count' => $v];
        }
        if ($is_output == 1) {
            $header = [['sku', 'sku'], ['count', 'count']];
            return Excel::exportData($merge_sku_new, $header, $brand_name, 'xlsx');
        }
        $data = [
            'code' => 1,
            'msg' => '',
            'count' => count($merge_sku_new),
            'data' => $merge_sku_new,
        ];
        return json($data);
    }


    public function array_unique_fb($array2D)
    {
        foreach ($array2D as $k => $v) {
            $v = join(',', $v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
            $temp[$k] = $v;
        }
        $temp = array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
        $temp = implode(',', $temp);
        $temp = explode(',', $temp);
        return  array_unique($temp);
    }
}
